package com.briup.day18;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author ThinkBook
 */
public class Test02_Student {
    //自定义集合中存储自定义类对象
    public static void main(String[] args) {
        Student s1 = new Student("zs",20);
        Student s2 = new Student("ls",21);
        Student s3 = new Student("ww",19);
        Student s4 = new Student("zl",22);

        Collection<Student> coll = new ArrayList<>();
        coll.add(s1);
        coll.add(s2);
        coll.add(s3);
        coll.add(s4);
        //加强for循环
        for(Student s : coll){
            System.out.println(s);
        }

        boolean f = coll.contains(s1);
        System.out.println("coll.contains(s1)" + f);

        Student s5 = new Student("zs",20);
       f = coll.contains(s5);
        System.out.println("coll.contains(s5)" + f);
    }
}
class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

}
